import {
  selectList,
  selectDetails,
  oilOperate,
  oilCardList,
  generatedBills,
  calHundredConsump,
  deleteOil,
  oilConfirm,
  selectAll,
  refreshOil,
} from '@/services/tmsOil';
import Prompt from '@/components/Prompt';

export default {
  namespace: 'tmsOil',
  state: {
    selectList: {},
    selectDetails: {},
    papersList: {},
    oilCardList: {},
  },

  effects: {
    *oilCardList({ payload,callback}, { call, put }) {
      const response = yield call(oilCardList, payload);
      if (response.code === 0) {
        const { list, pageSize, total, pageNum } = response.data;
        yield put({
          type: 'oilCard',
          payload: {
            oilCardList: {
              pagination: {
                current: pageNum,
                pageSize,
                total,
              },
              list,
            },
          },
        });
        callback && callback(list);
       

      }
    },
    *selectList({ payload, callback }, { call, put }) {
      const response = yield call(selectList, payload);
      if (response.code === 0) {
        const { list, pageSize, total, pageNum } = response.data;
        callback && callback(list);
        yield put({
          type: 'show',
          payload: {
            selectList: {
              pagination: {
                current: pageNum,
                pageSize,
                total,
              },
              list,
            },
          },
        });
      }
    },
    *selectDetails({ payload, callback }, { call, put }) {
      const response = yield call(selectDetails, payload);
      if (response.code === 0) {
        yield put({
          type: 'detail',
          payload: {
            selectDetails: { [payload.id]: response.data },
          },
        });
        callback && callback(response.data);
      }
    },
    *oilOperate({ payload, callback }, { call }) {
      const response = yield call(oilOperate, payload);
      const message = payload.id ? `编辑${response.message}` : `新增${response.message}`;
      if (response.code === 0) {
        Prompt({ content: message });
        if (callback) callback(response.data);
      }
    },

    *selectAll({ payload, callback }, { call }) {
      const response = yield call(selectAll, payload);
      if (response.code === 0) {
        Prompt({ content: `账单生成${response.message}!` });
        if (callback) callback(response.data);
      } else {
        if (callback) callback();
      }
    },
    //刷新油耗
    *refreshOil({payload,callback},{call,put}){
      const response=yield call(refreshOil,payload)
      const {data,code,message} = response
      if(code==0){
        Prompt({content: `刷新油耗${message}!`})
        if(callback) callback(data)
      }else{
        callback&&callback(data)
      }
      
     },
    /**
     * 生成账单
     */
    *generatedBills({ payload, callback }, { call }) {
      const response = yield call(generatedBills, payload);
      const { code, message, data } = response;
      if (code === 0) {
        Prompt({ content: `账单生成${message}!` });
        if (callback) callback(data);
      }
    },
    *calHundredConsump({ payload, callback }, { call }) {
      const response = yield call(calHundredConsump, payload);
      const { code, message, data } = response;
      if (code === 0) {
        if (callback) callback(data);
      }
    },
    *deleteOil({ payload, callback }, { call }) {
      const response = yield call(deleteOil, payload);
      const { code, message } = response;
      if (code === 0) {
        Prompt({ content: `删除${message}!` });
        if (callback) callback();
      }
    },
    *oilConfirm({ payload, callback }, { call }) {
      const response = yield call(oilConfirm, payload);
      const { code, message } = response;
      if (code === 0) {
        Prompt({ content: `确认${message}!` });
        if (callback) callback();
      }
    },
  },

  reducers: {
    oilCard(state, { payload }) {
      return {
        ...state,
        oilCardList: payload.oilCardList,
      };
    },
    show(state, { payload }) {
      return {
        ...state,
        selectList: payload.selectList,
      };
    },
    detail(state, { payload }) {
      return {
        ...state,
        selectDetails: { ...state.selectDetails, ...payload.selectDetails },
      };
    },
  },
};
